<template>
	<view>
		<view>
			<navbar @clickBackAction='clickBackAction' @clickSchedlAction='clickSchedlAction' ref="navbar">
			</navbar>
		</view>
		<view class="content-back" >
			<scroll-view class='leftSelect' scroll-y="true">
				<leftSelect @clickleftAction="clickleftAction" @clickBuyAction='clickBuyAction' v-if="showleftSelect">
				</leftSelect>
			</scroll-view>
			<view class="right-content">
				<caselist v-if="selectRow == 0 " ref="caselist" @clickCaselist='clickCaselist' @getUser="getUser">
				</caselist>
				<caseManage v-if="selectRow == 1 "></caseManage>
				<filelist v-if="selectRow == 2 " @clickCaselist='clickShowCaselist'></filelist>
				<worktime v-if="selectRow == 3 " @clickTeamShow='clickTeamShow'></worktime>
				<clientmanage v-if="selectRow == 4 "></clientmanage>
				<workteam v-if="selectRow == 5 "></workteam>
				<conflictquery v-if="selectRow == 6 "></conflictquery>
			</view>
		</view>
	</view>
</template>

<script>
	import navbar from '@/packageC/PhonePC/public/navbar.vue'
	import leftSelect from "@/packageC/PhonePC/public/leftSelect.vue"
	import caselist from "@/packageC/PhonePC/caseList/index.vue"
	import caseManage from "@/packageC/PhonePC/caseManage/index.vue"
	import filelist from "@/packageC/PhonePC/fileList/index.vue"
	import worktime from "@/packageC/PhonePC/workTime/index.vue"
	import clientmanage from "@/packageC/PhonePC/clientManage/index.vue"
	import workteam from "@/packageC/PhonePC/workTeam/index.vue"
	import conflictquery from "@/packageC/PhonePC/conflictQuery/index.vue"
	// import paramConfig from "@/packageB/PC/consultMange/paramConfig.vue"
	import verify from '@/pages/utils/verifyUtils.js'
	let app = ''
	export default {
		components: {
			navbar,
			leftSelect,
			caselist,
			caseManage,
			filelist,
			worktime,
			clientmanage,
			workteam,
			conflictquery
		},
		data() {
			return {
				role:'0',
				selectRow: 0,
				sessionKey: '',
				isPhone: false,
				showleftSelect: false
			}
		},
		onLoad() {
			app = getApp()
			this.loginRequest();

			//微信登录相关
			//this.wxLogin()
		},

		methods: {

			clickleftAction(e) {
				if (!this.isPhone) {
					uni.showModal({
						title: '提示',
						content: '请先绑定手机号',
						showCancel: false,
					})
				} else {
					this.selectRow = e.selectRow;
				}
			},
			wxLogin() {
				uni.showLoading({
					title: '正在登录...'
				})
				let _this = this
				uni.login({
					success: (res) => {
						console.log('获取code', res)
						_this.getLoginCode(res.code)

					},
					fail: (err) => {
						uni.hideLoading()
						uni.showToast({
							title: '登录失败，请退出重试！',
						})
					}
				})
			},
			async getLoginCode(code) {
				let _this = this
				const res = await _this.$myRequest({
					url: `/wx/ma/user/${app.globalData.appidKey}/login`,
					data: {
						code: code
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"Authorization": "Basic ZG90Z286ZG90Z28="
					},
				})
				console.log('获取loginCode', res)
				if (res.data.code == 200) {
					_this.sessionKey = res.data.data.sessionKey
					app.globalData.sessionKey = res.data.data.sessionKey
					_this.codeLogin(res.data.data)
				} else {
					uni.hideLoading()
					uni.showToast({
						title: '获取登录code失败，请退出重试！',
						icon: 'none',
					})
				}
			},

			async codeLogin(data) {
				console.log('手机里')
				let _this = this
				const res = await _this.$myRequest({
					url:`/oauth/login/token/maCode`,
					header:{
						"Content-Type":"application/x-www-form-urlencoded",
						"Authorization":"Basic ZG90Z286ZG90Z28=",
						// "from":"sys"
					},
					method: 'POST', //默认是GET
					data: {
						loginCode: data.loginCode,
						appidkey: app.globalData.appidKey
					}
				})
				uni.hideLoading()
				console.log('code登录', res)
				if (res.data.code == 200) {
					// _this.codeLogin(res.data.data)
					uni.setStorageSync("accessToken", res.data.data.accessToken)
					uni.setStorageSync("refreshToken", res.data.data.refreshToken)

					_this.getUser()
				} else if(res.data.code == 500){
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
					})
				}
				
				else {

					uni.showToast({
						title: '登录失败，请退出重试！',
						icon: 'none', 
					})
				}
			},

			//网络请求示例
			async loginRequest() {
				let _this = this
				//请求参数  admintest
				let parmas = {
					'username': 'admin',
					'password': '123456',
				}
				const res = await this.$myRequest({
					url: "/oauth/login/token/passwd",
					data: parmas,
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						"Authorization": "Basic ZG90Z286ZG90Z28=",
						"from":"sys"
					},
					method: 'POST', //默认是GET
				})
				console.log('返回结果', res)

				if (res.data.code == 200) {
					uni.setStorageSync("accessToken", res.data.data.accessToken)
					uni.setStorageSync("refreshToken", res.data.data.refreshToken)

					_this.getUser()
				}
			},
			
			//用户信息
			async getUser() {
				let _this = this

				const res = await _this.$myRequest({
					url: "/oauth/userinfo",
				})
				if (res.data.code == 200) {
					app.globalData.userInfo = res.data.data
					app.globalData.role = res.data.data.userType.code
//此 实际情况以登录返回为准
					_this.role = app.globalData.role
					let mobile = res.data.data.mobile
if(mobile==null || mobile== '' || mobile==undefined || mobile.length<11){
						_this.isPhone = false
						app.globalData.isPhone = false
					}else{
						app.globalData.userInfo.mobile = mobile
						app.globalData.isPhone = true
						_this.isPhone = true
						
					}
					app.globalData.userInfo = res.data.data
					
					_this.selectRow = 0
					_this.showleftSelect = true
					_this.$refs.caselist.loginRequestData();
					_this.$refs.navbar.getRole()			
					
					
					//普通用户  调用普通用户的接口
					if(_this.role == '0'){
						console.log('普通用户')
						// _this.getAdv()
						// if(_this.isPhone){
						// 	_this.getCaseList()
						// }
						
					}
					
								
					
				}else{
					uni.showToast({
						title: res.data.msg,
						
					})
				}
			},
			/**
			 * 点击了获取手机号
			 */
			getphonenumber(e){
				console.log(e)
				let code = e.detail.code
				let encryptedData = e.detail.encryptedData
				let errMsg = e.detail.errMsg
				let iv = e.detail.iv
				
				let _this = this
				
				_this.getWxPhone(encryptedData,iv,code)
				
				
			
			},
			async getWxPhone(data1,data2,data3){
				let _this = this
				uni.showLoading({
					
				})
				const res = await _this.$myRequest({
					url:`/wx/ma/user/${app.globalData.appidKey}/phone`,
					data:{
						sessionKey:_this.sessionKey,
						encryptedData:data1,
						iv:data2,
						code:data3
					}
				})
				uni.hideLoading()
				console.log('获取手机号',res)
				if(res.data.code==200){
					app.globalData.userInfo.mobile = res.data.data.phoneNumber
					_this.isPhone = true
					app.globalData.isPhone = true
				}
				
				
			},
			//点击案件列表
			clickCaselist(e) {
				console.log('SDK' + e)
				uni.navigateTo({
					url: 'caseList/caseDetail/caseDetail?id=' + e.id,
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						console.log(err)
					},
					complete: () => {
					}
				})
			},

			//刷新案件列表
			refreshCaseListData() {
				this.$refs.caselist.requestData();
			},
			//文件列表查看案件详情
			clickShowCaselist(e) {
				uni.navigateTo({
					url: 'caseList/caseDetail/caseDetail?id=' + e.caseId,
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						console.log(err)
					},
					complete: () => {

					}
				})
			},
			//文件列表查看案件详情
			clickTeamShow(e) {
				if (!e.caseId) {
					uni.showToast({
						title: '没有关联案件！',
						icon: 'none'
					})
					return;
				}
				uni.navigateTo({
					url: 'caseList/caseDetail/caseDetail?id=' + e.caseId,
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						console.log(err)
					},
					complete: () => {

					}
				})
			},
			
			//点击日程
			clickSchedlAction() {
				uni.navigateTo({
					url: 'schedule/index'
				})
			},

			//后台管理
			clickBackAction() {
				uni.navigateTo({
					url: '/packageB/PC/index'
				})
			},

			//购买服务
			clickBuyAction() {
				uni.navigateTo({
					url: 'buyServe/buyList'
				})
			}

		}
	}
</script>

<style>
	.content-back {
		display: flex;
		width: 100%;
	}

	.leftSelect {
		width: 16%;
		max-width: 200px;
		height: 92vh;
		border-right: 1px #eee solid;
		border-radius: 5px;
		background-color: white;
	}

	.right-content {
		width: 84%;
		min-width: 84%;
		background-color: white;

	}
</style>
